import json
import csv
import time
import requests
from application_info import getall_uri_list
from appinfo import get_appInfo
from comment import get_comment

static_count = 0

url1 = 'https://web-drcn.hispace.dbankcloud.cn/uowap/index'

ua = {
    "User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36'
}




uri_list = getall_uri_list()
for u  in uri_list:
    tabname_list = (list)(u.keys())
    tabname = tabname_list[0]

    uri_list = (list)(u.values())#这个list只有一个元素 即tabid
    uri = uri_list[0]
    reqPageNum = 1
    params = {
        'method': 'internal.getTabDetail',
        'serviceType': '20',
        'reqPageNum': f'{reqPageNum}',
        'uri': f'{uri}',
        'maxResults': '25',
        'zone': '',
        'locale': 'zh'
    }

    response = requests.get(url=url1, headers=ua, params=params)
    response.encoding = 'utf-8'
    json_list = (json.loads(response.text))
    response.close()

    # 从JSON字符串中剥离数据 获取APP info
    datalist = json_list.get('layoutData')
    dict = datalist.pop(0)
    app_list = dict.get('dataList')
    # print(app_list)
    # 存放剥离后的APP info 数据
    appinfo_list = []
    appid_list = []
    while (app_list != None):

        for i in app_list:
            # 每个APPinfo 放在一个dict 中 再存入list
            appinfo_dict = {}
            appinfo_dict['appid'] = i.get('appid')
            #所有id存入list中 留待 调用appinfo中 获取APP信息
            appid_list.append(i.get('appid'))
            appinfo_dict['name'] = i.get('name')
            appinfo_dict['icon'] = i.get('icon')
            appinfo_dict['version'] = i.get('appVersionName')
            appinfo_dict['size'] = i.get('sizeDesc')
            appinfo_dict['downcount'] = i.get('downCountDesc')
            appinfo_dict['kindname'] = i.get('kindName')
            appinfo_dict['tagname'] = i.get('tagName')
            appinfo_list.append(appinfo_dict)
        reqPageNum = reqPageNum + 1
        params = {
            'method': 'internal.getTabDetail',
            'serviceType': '20',
            'reqPageNum': f'{reqPageNum}',
            'uri': f'{uri}',
            'maxResults': '25',
            'zone': '',
            'locale': 'zh'
        }

        response = requests.get(url=url1, headers=ua, params=params)
        response.encoding = 'utf-8'
        json_list = (json.loads(response.text))
        response.close()
        #time.sleep(0.2)
        # 从JSON字符串中剥离数据 获取APP info
        datalist = json_list.get('layoutData')
        # print(datalist)
        if len(datalist) != 0:
            dict = datalist.pop(0)
        else:
            dict = {}
        app_list = dict.get('dataList')
    filename = 'classList/' + tabname + '.csv'
    f = open(filename, mode="a", encoding="utf-8", newline="")
    # 文件是 append模式，之前的旧数据会留在里面 这里先将游标定位在文首，然后用truncate清空 再添加
    f.seek(0)
    f.truncate()
    csvwriter = csv.writer(f)
    for i in appinfo_list:
        csvwriter.writerow(i.values())
    f.close()
    #统计数据条数
    print(len(appid_list))
    static_count = len(appid_list) + static_count
    filename = 'appid.csv'
    f1 = open(filename, mode="a", encoding="utf-8", newline="")
    csvwriter = csv.writer(f1)
    csvwriter.writerow(appid_list)
    #调用函数 获取所有APP信息
    #get_appInfo(appid_list)
    #get_comment(appid_list)


print(static_count)
















# #从JSON字符串中剥离数据 获取APP info
# datalist = json_list.get('layoutData')
# dict = datalist.pop(0)
# app_list = dict.get('dataList')
# #print(app_list)
# #存放剥离后的APP info 数据
# appinfo_list = []
# while (app_list != None):
#
#     for i in app_list:
#         # 每个APPinfo 放在一个dict 中 再存入list
#         appinfo_dict = {}
#         appinfo_dict['appid'] = i.get('appid')
#         appinfo_dict['name'] = i.get('name')
#         appinfo_dict['icon'] = i.get('icon')
#         appinfo_dict['size'] = i.get('sizeDesc')
#         appinfo_dict['downcount'] = i.get('downCountDesc')
#         appinfo_dict['kindname'] = i.get('kindName')
#         appinfo_dict['tagname'] = i.get('tagName')
#         appinfo_list.append(appinfo_dict)
#     reqPageNum = reqPageNum + 1
#     params = {
#         'method': 'internal.getTabDetail',
#         'serviceType': '20',
#         'reqPageNum': f'{reqPageNum}',
#         'uri': '33ef450cbac34770a477cfa78db4cf8c',
#         'maxResults': '25',
#         'zone': '',
#         'locale': 'zh'
#     }
#     response = requests.get(url=url1, headers=ua, params=params)
#     response.encoding = 'utf-8'
#     json_list = (json.loads(response.text))
#     # 从JSON字符串中剥离数据 获取APP info
#     datalist = json_list.get('layoutData')
#     #print(datalist)
#     if len(datalist) != 0:
#         dict = datalist.pop(0)
#     else:
#         dict = {}
#     app_list = dict.get('dataList')


# filename = 'appData/热门.csv'
# f= open(filename,mode="a",encoding="utf-8",newline="")
# #文件是 append模式，之前的旧数据会留在里面 这里先将游标定位在文首，然后用truncate清空 再添加
# f.seek(0)
# f.truncate()
# csvwriter = csv.writer(f)
# for i in appinfo_list:
#     csvwriter.writerow(i.values())
#print(appinfo_list)




